define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'commission/sale/index' + location.search,
                    // add_url: 'commission/sale/add',
                    // edit_url: 'commission/sale/edit',
                    // del_url: 'commission/sale/del',
                    multi_url: 'commission/sale/multi',
                    import_url: 'commission/sale/import',
                    table: 'customer_package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: (params) => {
                    var filter = JSON.parse(params.filter)
                    var op = JSON.parse(params.op)
                    filter.identity = 0
                    op.identity = '='
                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)
                    return params
                },
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id', title: __('id'),
                            width: 100
                        },
                        {
                            field: 'nickname',
                            title: __('销售名称'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'year_month_str',
                            title: __('月份'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'total_assembly',
                            title: __('总提成'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('提成明细'),
                                    title: __('提成明细'),
                                    extend: 'data-area=\'["80%","80%"]\'',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'commission/sale/log?year_month={year_month}',
                                },
                            ],
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                onLoadSuccess: function () {
                    var cookiefilenames = 'customer_index_controller';  //对应控制器列表页
                    table.on('column-switch.bs.table', function (e, json) {
                        var hideColumns = [];
                        $.each(table.bootstrapTable('getHiddenColumns'), function (i, item) {
                            hideColumns.push(item.field);
                        });
                        $.cookie('texie_' + cookiefilenames, JSON.stringify(hideColumns), {expires: 365, path: '/'});
                    });
                    if ($.cookie('texie_' + cookiefilenames)) {
                        $.each(JSON.parse($.cookie('texie_' + cookiefilenames)), function (i, item) {
                            table.bootstrapTable('hideColumn', item);
                        });
                    }
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".month-item").click(function () {
                $(".month-item").removeClass('action')
                $(this).addClass('action')
                const index = $(this).attr('index')
                $('#month').val(index)
                refresh()
            })
            $('#year').change(function () {
                refresh()
            })
            function refresh() {
                const year = $('#year').val()
                const month = $('#month').val()
                table.bootstrapTable('refresh', {
                    url: $.fn.bootstrapTable.defaults.extend.index_url + '&year_month=' + year + '-' + month
                });
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        log: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'commission/sale/log' + location.search,
                    // add_url: 'commission/accounting/add',
                    edit_url: 'commission/sale/edit/year_month/'+Config['year_month'],
                    // del_url: 'commission/accounting/del',
                    multi_url: 'commission/sale/multi',
                    import_url: 'commission/sale/import',
                    table: 'customer_package',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // fixedColumns: true,
                // fixedRightNumber: 2,
                queryParams: (params) => {
                    // var filter = JSON.parse(params.filter)
                    // var op = JSON.parse(params.op)
                    // filter.identity = 1
                    // op.identity = '='
                    // params.filter = JSON.stringify(filter)
                    // params.op = JSON.stringify(op)
                    return params
                },
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id', title: __('id'),
                            width: 100
                        },
                        {
                            field: 'customer.full_name',
                            title: __('客户名称'),
                            operate: 'LIKE',
                            width: 100,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'customer.idnumber',
                            title: __('身份证号码'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'customer.corporate_name',
                            title: __('公司名称'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        {
                            field: 'service_name',
                            title: __('服务名称'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'start_time',
                            title: __('服务开始时间'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'end_time',
                            title: __('服务结束时间'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'sales_commission_type',
                            title: __('提成结算方式'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            custom: {1: "green", 2: "orange"},
                            searchList: {0: '(成交价格-成本价格)*%', 1: '固定金额', 2: '每月提成'},
                            class: 'autocontent',
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'sales_commission',
                            title: __('提成金额'),
                            operate: 'LIKE',
                            width: 200,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                onLoadSuccess: function () {
                    var cookiefilenames = 'customer_index_controller';  //对应控制器列表页
                    table.on('column-switch.bs.table', function (e, json) {
                        var hideColumns = [];
                        $.each(table.bootstrapTable('getHiddenColumns'), function (i, item) {
                            hideColumns.push(item.field);
                        });
                        $.cookie('texie_' + cookiefilenames, JSON.stringify(hideColumns), {expires: 365, path: '/'});
                    });
                    if ($.cookie('texie_' + cookiefilenames)) {
                        $.each(JSON.parse($.cookie('texie_' + cookiefilenames)), function (i, item) {
                            table.bootstrapTable('hideColumn', item);
                        });
                    }
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

    };
    return Controller;
});
